package com.dcivision.dms.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.alert.bean.UpdateAlertSystemLog;
import com.dcivision.alert.dao.UpdateAlertDAObject;
import com.dcivision.alert.dao.UpdateAlertSystemLogDAObject;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.dao.DmsDocumentDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;

public class MaintDmsViewInvitationAction extends AbstractMaintAction {
  public static final String REVISION = "$Revision: 1.4.4.1 $";
  public MaintDmsViewInvitationAction(){
   super(); 
  }
  public String getFunctionCode() {
    return (SystemFunctionConstant.DMS_PUBLIC_FOLDER);
  }

  public String getMajorDAOClassName() {
    return null;
  }
  

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    AbstractActionForm maintForm = (AbstractActionForm)form;
    String redrectUrl = null;
    try{
      redrectUrl = this.getForwodUrl(mapping, maintForm, request, response);
      if (redrectUrl==null){
        return mapping.findForward("R");
      }else{
        response.sendRedirect(this.getForwodUrl(mapping, maintForm, request, response));
      }
      return mapping.findForward(null);
    }catch(Exception e){      
      log.error(e, e);
      return mapping.findForward("R");
    }    
  }  
  
  
  public String getForwodUrl(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintDmsViewInvitationForm maintForm = (MaintDmsViewInvitationForm)form;
    
    String forwordUrl = null;
    String updateAlertID = "";
    String objectID = "";    
    Integer updateAlertIDObj = null;
    Integer objectIDObj = null;
    
    if(maintForm != null){
      updateAlertID = maintForm.getUpdateAlertID();
      objectID =  request.getParameter("objectID");
    }else{
      updateAlertID = request.getParameter("updateAlertID");  
      objectID =  request.getParameter("objectID");
    }
    
   
    String appBaseURL = getApplicationBaseURL();
    
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(this.getSessionContainer(request),this.getConnection(request));    
    DmsDocumentDAObject dmsDocmentDAO = new DmsDocumentDAObject(this.getSessionContainer(request),this.getConnection(request));    
    UpdateAlertDAObject updateAlertTypeDAO = new UpdateAlertDAObject(this.getSessionContainer(request),this.getConnection(request));
//    UpdateAlertSystemLogDAObject alertSystemDAO = new UpdateAlertSystemLogDAObject(this.getSessionContainer(request),this.getConnection(request));
    /*
     * Update Code Start
     * Author			Jim.Zhou
     * Date				2006/04/28
     * Bug				#5264
     */
    //UpdateAlertTypeDAObject updateAlertTypeDAObject = new UpdateAlertTypeDAObject(this.getSessionContainer(request),this.getConnection(request));
    UpdateAlertSystemLogDAObject updateAlertSystemLogDAObject = new UpdateAlertSystemLogDAObject(this.getSessionContainer(request),this.getConnection(request));
    /*
     * Update Code End
     */
    try{    
      if(!Utility.isEmpty(updateAlertID)){
        updateAlertIDObj = TextUtility.parseIntegerObj(updateAlertID);
//        UpdateAlertSystemLog alertSystemLog=(UpdateAlertSystemLog)alertSystemDAO.getObjectByID(updateAlertSystemLogIDObj);      
        UpdateAlert updateAlertType=(UpdateAlert)updateAlertTypeDAO.getObjectByID(updateAlertIDObj);
        if( updateAlertType == null || "I".equals(updateAlertType.getRecordStatus()) ){
          return null;          
        }
        if(Utility.isEmpty(objectID)){
          objectIDObj = updateAlertType.getObjectID();  
        }else{
          objectIDObj = TextUtility.parseIntegerObj(objectID);
        }
                
        DmsDocument dmsDocument= (DmsDocument)dmsDocmentDAO.getObjectByID(objectIDObj);
        if( dmsDocument == null){
          return null;          
        }  
        /*
         * Update Code Start
         * Author			Jim.Zhou
         * Date				2006/04/28
         * Bug				#5264
         */
        /*
        List updateAlertTypeList = updateAlertTypeDAObject.getByUpdateAlertIDByOwnerID(updateAlertIDObj,"I",null);
        Iterator updateAlertTypeListIterator = updateAlertTypeList.iterator();
        Integer id = null;
        while (updateAlertTypeListIterator.hasNext()){
        	UpdateAlertType updateAlertTypeBean = (UpdateAlertType)updateAlertTypeListIterator.next();
        	id = updateAlertTypeBean.getID();
        }
         */
        Integer updateAlertSystemLogID = null;
    	UpdateAlertSystemLog updateAlertSystemLog = (UpdateAlertSystemLog)updateAlertSystemLogDAObject.getSystemLogByUpdateAlertIDActionType(updateAlertIDObj,"I");
    	if (!Utility.isEmpty(updateAlertSystemLog)){
        	updateAlertSystemLogID = updateAlertSystemLog.getID();
    	}
        /*
         * Update Code End
         */        

        String sCurFunctionCode = docRetrievalManager.getCurFunctionCode(dmsDocument);      
       
        forwordUrl= appBaseURL+"/dms/MaintDmsDocument.do?documentType=" + dmsDocument.getDocumentType() + "&rootID=" + dmsDocument.getRootID() + "&parentID="
        + dmsDocument.getID() + "&sCurFunctionCode=" + sCurFunctionCode + "&navMode=" + GlobalConstant.NAV_MODE_PREVIEW + "&ID=" + dmsDocument.getID() + "&objectID=" + dmsDocument.getID()+"&fromEmail=true";
        forwordUrl += "&invitation=true&updateAlertID="+updateAlertID;
        forwordUrl=forwordUrl+"&updateReadOperation="+"true";          
        /*
         * Update Code Start
         * Author			Jim.Zhou
         * Date				2006/04/28
         * Bug				#5264
         */
        if (!Utility.isEmpty(updateAlertSystemLogID)){
            forwordUrl = forwordUrl + "&updateAlertSystemLogID="+updateAlertSystemLogID;
        } else {
        	forwordUrl = forwordUrl + "&updateAlertSystemLogID=";
        }
//        forwordUrl = forwordUrl + "&updateAlertSystemLogID="+updateAlertSystemLogID+"&updateReadOperation="+"true";        
        /*
         * Update Code End
         */
//        forwordUrl = forwordUrl + "&updateAlertSystemLogID="+updateAlertSystemLogID+"&updateReadOperation="+"true";        
      }    
    }catch(Exception e){
      log.error(e, e);
      forwordUrl = null;
    }
    
    return forwordUrl;
  }  
  
  
  protected String getApplicationBaseURL(){
    return SystemParameterFactory.getSystemParameter(SystemParameterConstant.APPLICATION_BASE_URL);
  }  
  
}
